//index.js
const { url } = require('../../config/index.js') // 接口请求地址
const { commonLogin, getLocation, jumpWebView, getUserPhone } = require('../../utils/util.js') // 接口请求地址


const app = getApp() //获取应用实例

Page({
  data: {
    userInfo: {}, // 用户信息
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    userLocationInfo: {}, // 用户信息
    // banner-data
    imgUrls: {},
    swiperIndex: 0,
    indicatorDots: true, // banner定位点
    autoplay: true, // 自动滚动
    interval: 3000, // 切换间隔
    duration: 500 // 切换速度
  },
  login () {
    // 登录授权
    commonLogin().then(res => {
      console.log(res)
      // 未绑定手机去绑定手机
      if (res.IS_BINDING === "0") {
        wx.showModal({
          title: '提示',
          content: '您未绑定手机，请前往绑定',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/changePhone/changePhone?type=first'
              })
            } else if (res.cancel) {
            }
          }
        })
      }
    })
  },
  // 获取首页banner图
  bannerGet () {
    wx.request({
      url: url + 'ServComment/getSmHomeImages.action',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        CITY: '深圳'
      },
      success: res => {
        console.log('返回banner信息')
        console.log(res.data)
        if (res.data.STATUS_CODE === 1) {
          this.setData({
            imgUrls: res.data.RESULT_DATA
          })
        }
      },
      fail: err => {
        console.log('返回banner信息ERR')
        console.log(err)
      }
    })
  },
  // banner点击
  bannerTap (e) {
    let url = e.currentTarget.dataset.item.WEBHREF
    let search, searchs, all, shopNames;
    if (url.indexOf('?') > -1) {
      search = url.split('?')[1];
    }
    if (url.indexOf('=') > -1) {
      searchs = search.split("&");
      if (searchs.length > 0) {
        all = searchs[0].split('=')[1];
      }
      if (searchs.length > 1) {
        shopNames = searchs[1].split('=')[1];
      }
    }
    // 直接跳转到当前店
    if (all === 'banner' || all === 'all' || all != undefined) {
      this.forFood()
      return;
    }
    //直接跳转到外部活动
    wx.navigateTo({
      url: '/pages/webView/webView?url=' + url,
    })
  },
  // 红包优惠
  redBag () {
    getUserPhone()
      .then(res => {
        let redUrl = 'https://carnt.carnettong.com/CARNT/act/chit/index_xcx.jsp?from=xcx&fromPhone=' + res.PHONE
        // let redUrl = 'https://carnt.carnettong.com/CARNT/act/chit/index_xcx.jsp?from=xcx&fromPhone=' + 18312580999
        jumpWebView(redUrl, '新人红包')
      })
  },
  // 点餐
  forFood () {
    wx.switchTab({
      url: '/pages/white/white'
    })
  },
  // 咖啡文化
  cafeCulture () {
    let Url = 'https://carnt.carnettong.com/4SCAFE/HTML5/aTestWx/dist/index.html?enterway=xcx#/cafeCulture'
    jumpWebView(Url, '咖啡文化')
  },
  // 积分商城
  pointMall () {
    wx.navigateTo({
      url: '/pages/pointMall/pointMall',
    })
  },
  onLoad: function () {
    // 登录
    // this.login()
    // 定位
    getLocation()
    // 获取首页banner图
    this.bannerGet()
  },
  onReady () {
  },
  onShow (options) {
    this.login()
    
    // 检查微信登录是否过期
    /*wx.checkSession({
      success: () => {
        //session 未过期，并且在本生命周期一直有效
        console.log('登录未过期')
        this.login()
      },
      fail: () => {
        console.log('登录过期')
        // 重新登录
        this.login()
      }
    })
    */
    // 保存一个标识，用于从点餐tab页返回时跳转
    wx.setStorageSync('whereToJump', 'index')
    // 删除unLoadWhite标识， 用于进入点餐tab页跳转
    wx.removeStorageSync('unLoadWhite')
  }
})
